package com.zego.wawaji.ui.center.question;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.center.question.adapter.MyQuestionAdapter;
import com.zego.wawaji.ui.center.question.bean.QuestionList;

/**
 * 我的反馈
 * Created by Su on 2018/4/11.
 */
public class QuestionMyAct extends BaseAppCompatActivity implements RequestComplete, PObserver {
    private CustomRecyclerView custom_layout;
    private RecyclerView recyclerView;
    private MyQuestionAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_my_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("我的反馈");

        MsgMgr.getInstance().attach(this);
        custom_layout = (CustomRecyclerView) findViewById(R.id.custom_layout);
        custom_layout.showNoData();
        recyclerView = custom_layout.getRecyclerView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ItemSpaces());
        adapter = new MyQuestionAdapter();
        recyclerView.setAdapter(adapter);
        ModuleMgr.getWawaMgr().reqMyQuestion(this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqMyQuestion) {
            if (!response.isOk()) {
                return;
            }
            QuestionList data = (QuestionList) response.getBaseData();
            if (data == null || data.getQuestionList().size() <= 0) return;

            adapter.setList(data.getQuestionList());
            custom_layout.showRecyclerView();
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Refresh_Question:
                ModuleMgr.getWawaMgr().reqMyQuestion(this);
                break;
        }
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.top = 30;
            outRect.right = 40;
            outRect.left = 40;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}
